/*
*  Copyright 2012 m4f10
*
*  This file is part of uScan.
*  uScan is free software: you can redistribute it and/or modify it under the
*  terms of the GNU General Public License as published by the 
*  Free Software Foundation, either version 3 of the License, or (at your option)
*  any later version.
*
*    uScan is distributed in the hope that it will be useful, 
*    but WITHOUT ANY WARRANTY; without even the implied warranty of 
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* 
*    See the GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License 
*    along with uScan. If not, see http://www.gnu.org/licenses/.
*/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uscan;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author m4f10
 */
public class UScanProgress extends Thread {
    private int total_ins;
    private int current_ins;
    protected UScanProgress thread;
    protected Main form;
    
    public UScanProgress(Main form, int total_ins)
    {
        this.form = form;
        setCurrentInstruction(8);
        setTotalInstruction(total_ins);
        form.pProgress.setEnabled(true);
        
        form.pProgress.setMinimum(0);
        form.pProgress.setMaximum(100);
    }
    
    public void run()
    {
        while(!isFinished())
        {
            form.pProgress.setValue((int)((getCurrentInstruction()*100)/getTotalInstruction()+1));
            try {
                Thread.sleep(10);
                form.pProgress.repaint();
            } catch (InterruptedException ex) {
                Logger.getLogger(UScanProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
    public int getTotalInstruction()
    {
        return this.total_ins;
    }            
    public int getCurrentInstruction()
    {
        return this.current_ins;
    }
    
    public void setTotalInstruction(int ins)
    {
        if(ins>0)
        {
            this.total_ins = ins;
        }
    }
    public void setCurrentInstruction(int ins)
    {
        if(ins>0)
        {
            this.current_ins = ins;
        }
    }
    public void addToCurrentInstruction(int ins)
    {
        if(ins+getCurrentInstruction()<=getTotalInstruction())
        {
            this.current_ins += ins;
        }
    }
    
    public boolean isFinished()
    {
        if(getCurrentInstruction()>=getTotalInstruction())
        {
            System.out.println("UScanProgress :: Finished monitoring UScan!");
            return true;
        }
        return false;
    }
    
    public void forceFinish()
    {
        this.setCurrentInstruction(this.getTotalInstruction());
    }
    
    
    public void startMonitoring()
    {
        thread = this;
        thread.start();
    }
    
    
}
